export default {
	data() {
		return {
			orderType: '',
			shopList: [],
			order: '',
			sort: 'desc',
			keyword: '',
			lat: 0,
			lng: 0,
			iphoneX: false,
			hasFollow: false,
			is_open1: 0,
			is_open2: 0,
			industryList: [], //主营行业
			industry: {
				id: '',
				name: '全部行业'
			},
			sorting: {
				sort_showname: '距离从近到远',
				sort_type: 'asc',
				sort_name: 'distance'
			},
			city: {
				id: 0,
				name: '全国'
			}
		}
	},

	onLoad(options) {
		this.keyword = options.keyword || '';
		this.iphoneX = this.$util.uniappIsIPhoneX()
		this.Getindustry()
		

	},
	onShow() {
		// 刷新多语言
		this.$langConfig.refresh();
		if (uni.getStorageSync('location')) {
		 	this.lat = uni.getStorageSync('location').latitude;
		 	this.lng = uni.getStorageSync('location').longitude;
		} else {
		
		}
		if (uni.getStorageSync('city')) {
			this.city.id = uni.getStorageSync('city').id;
			this.city.name = uni.getStorageSync('city').title;
		} else {
			uni.setStorageSync('city', {
				id: 0,
				title: '全国'
			});
			this.city.id = 0
			this.city.name = '全国';
		}
		//this.GetLatLong()
	},
	methods: {
		// 获取当前经纬度
		GetLatLong() {
			let self = this
			uni.getLocation({
				type: 'gcj02',
				success: (res) => {
					self.lng = res.longitude;
					self.lat = res.latitude;
					if(res.longitude && res.latitude){
						self.$refs.mescroll.refresh();
					}else{
						self.GetLatLong()
					}
					console.log('当前位置', res);
				},
				fail:(res) => {
					console.log('获取经纬度失败', res);
				},
			});
		},
		follow() {
			if (!uni.getStorageSync('token')) {
				this.$util.redirectTo('/pages/login/login/login', {});
				return;
			}
			if (this.hasFollow) {
				uni.showModal({
					title: '提示',
					content: '真的要取消关注嘛',
					success: function(res) {
						if (res.confirm) {
							this.$api.sendRequest({
								url: '/api/shopmember/delete',
								data: {
									site_id: this.shopList.site_id
								},
								success: res => {
									if (res.code == -1 && res.error_code == 'TOKEN_ERROR') {
										this.$util.showToast({
											'title': '登录失败'
										});
										let pageRouter = getCurrentPages();
										let data = pageRouter[pageRouter.length - 1].options;
										let back = pageRouter[pageRouter.length - 1].route;
										data.back = back;
										setTimeout(() => {
											this.$util.redirectTo('/pages/login/login/login', data);
										}, 1500)
										return;
									}
									if (res.code == 0 && res.data) {
										this.hasFollow = !this.hasFollow;
										this.$util.showToast({
											title: '取消成功'
										});
									}
								}
							});
						}
					}
				});
			} else {
				this.$api.sendRequest({
					url: '/api/shopmember/add',
					data: {
						site_id: this.shopList.site_id
					},
					success: res => {
						if (res.code == 0 && res.data) {
							this.hasFollow = !this.hasFollow;
							this.$util.showToast({
								title: '关注成功'
							});
						}
					}
				});
			}
		},
		isFollow() {
			this.$api.sendRequest({
				url: '/api/shopmember/issubscribe',
				data: {
					site_id: this.shopList.site_id
				},
				success: res => {
					if (res.code == 0) {
						this.hasFollow = res.data;
					}
				}
			});
		},
		getShopList(mescroll) {
			let self = this
			if(self.lng && self.lat){
				this.getShopListData(mescroll)
			}else{
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						self.lng = res.longitude;
						self.lat = res.latitude;
						//if(self.lng && self.lat){
							this.getShopListData(mescroll)
						// }else{
						// 	this.getShopList()
						// }
					},
					fail:(res) => {
						uni.showToast({
							icon: 'none',
							position: 'center',
						    title: '获取经纬度失败',
						    duration: 10000
						});
						this.getShopListData(mescroll)
						console.log('获取经纬度失败', res);
					},
				});
			}
			
		},
		getShopListData(mescroll) {
			this.$api.sendRequest({
				url: '/api/shop/page',
				data: {
					page: mescroll.num,
					page_size: mescroll.size,
					keyword: this.keyword,
					lat: this.lat,
					lng: this.lng,
					order: this.sorting.sort_name, //排序参数
					sort: this.sorting.sort_type, //升序降序
					city: this.city.id, //城市ID
					category_id: this.industry.id //主营行业id
				},
				success: res => {
					//console.log(res)
					//console.log(this.lat);
					//console.log(this.lng);
					let newArr = []
					let msg = res.message;
					if (res.code == 0 && res.data) {
						newArr = res.data.list;
						for (var i = 0; i < newArr.length; i++) {
							newArr[i].composite_score = ((parseFloat(newArr[i].shop_desccredit) + parseFloat(newArr[i].shop_servicecredit) +
								parseFloat(newArr[i].shop_deliverycredit)) / 3).toFixed(1);
						}
						//console.log(res.data.list)
					} else {
						this.$util.showToast({
							title: msg
						})
					}
					mescroll.endSuccess(newArr.length);
					//设置列表数据

					if (mescroll.num == 1) this.shopList = []; //如果是第一页需手动制空列表
					this.shopList = this.shopList.concat(newArr); //追加新数据
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				},
				fail: res => {
					mescroll.endErr();
					if (this.$refs.loadingCover) this.$refs.loadingCover.hide();
				}
			});
		},
		//筛选点击
		sortTabClick(tag) {
			if (tag == 1) {
				if (this.is_open2 == 1) {
					this.is_open2 = 0
					this.$refs.conditions_popup.close()
				}
				if (this.is_open1 == 0) {
					this.$refs.works_popup.open()
					this.is_open1 = 1
				} else {
					this.$refs.works_popup.close()
					this.is_open1 = 0
				}
			} else {
				if (this.is_open1 == 1) {
					this.is_open1 = 0
					this.$refs.works_popup.close()
				}
				if (this.is_open2 == 0) {
					this.$refs.conditions_popup.open()
					this.is_open2 = 1
				} else {
					this.$refs.conditions_popup.close()
					this.is_open2 = 0
				}
			}
		},
		// 选择主营行业
		industry_check(event) {
			this.industry.id = event.category_id
			this.$refs.works_popup.close()
			this.is_open1 = 0
			this.industry.name = event.category_name
			this.$refs.mescroll.refresh();
		},
		conditions_check(type, name, order) {
			this.sorting.sort_type = order //排序
			this.sorting.sort_name = name //分类
			if (type == 1) {
				if (this.sorting.sort_type == 'asc') {
					this.sorting.sort_showname = '销量从高到低'
				} else {
					this.sorting.sort_showname = '销量从低到高'
				}
			} else if (type == 2) {
				if (this.sorting.sort_type == 'asc') {
					this.sorting.sort_showname = '距离从近到远'
				} else {
					this.sorting.sort_showname = '距离从远到近'
				}
			} else {
				if (this.sorting.sort_type == 'asc') {
					this.sorting.sort_showname = '评分从高到低'
				} else {
					this.sorting.sort_showname = '评分从低到高'
				}
			}
			this.is_open2 = 0
			this.$refs.conditions_popup.close()
			this.$refs.mescroll.refresh();
		},
		change(event) {
			if (event.show == false) {
				this.is_open1 = 0
				this.is_open2 = 0
			}
		},
		reset(e) {
			if (e == 1) {
				this.industry.id = ''
				this.industry.name = '全部行业'
				this.$refs.mescroll.refresh();
				this.$refs.works_popup.close()
				this.is_open1 = 0
			} else {
				this.sorting.sort_name = 'distance'
				this.sorting.sort_type = 'asc'
				this.sorting.sort_showname = '距离从近到远'
				this.is_open2 = 0
				this.$refs.conditions_popup.close()
				this.$refs.mescroll.refresh();
			}
		},
		to_search() {
			this.$util.redirectTo('/otherpages/shop/street/newstreet');
			// this.$refs.mescroll.refresh();
		},
		search() {
			this.$refs.mescroll.refresh();
		},
		imageError(index) {
			this.shopList[index].logo = this.$util.getDefaultImage().default_shop_img;
			this.$forceUpdate();
		},
		phone(e) {
			uni.makePhoneCall({
				phoneNumber: e //仅为示例
			});
		},
		jump(e) {
			this.$util.redirectTo(e)
		},
		GetDistance(lat1, lng1, lat2, lng2) {
			var radLat1 = lat1 * Math.PI / 180.0;
			var radLat2 = lat2 * Math.PI / 180.0;
			var a = radLat1 - radLat2;
			var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
			var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
				Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
			s = s * 6378.137; // EARTH_RADIUS;
			s = Math.round(s * 10000) / 10000;
			return s;
		},
		Getindustry() {
			this.$api.sendRequest({
				url: '/api/shop/shopCategory',
				success: res => {
					if (res.code == 0 && res.data) {
						this.industryList = res.data
						this.industryList.unshift({
							category_id: '',
							category_name: '全部行业'
						})
					}
				}
			});
		}
	}
}
